<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'跟进提醒'),
	array('op'=>'edit','title'=>'添加提醒'),
);

//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_member_notice',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
			$item['content']=json_decode($item['content'],true);
			$item['content']=$item['content']['head'];
			$item['lookarea2']='';
			foreach($this->work_lookareas as $k=>$v){
				if(strpos($item['lookarea'],';'.$k.';') !== false){ 
					$item['lookarea2'] .=$v.',';
				}
			}
			$item['timebox']=json_decode($item['timebox'],true);
			$item['sendtime']='';
			foreach($item['timebox'] as $v){
				$item['sendtime'] .=$v['time'].',';
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：使用场景：一方面可以有效的帮助销售人员把握跟单节奏，系统会自动提醒销售该跟进的客户，让销售做好合理的安排调度，再也不用担心因为工作忙而忘记跟进客户，一方面也有利于管理层每日对销售人员客户跟进的情况了如指掌，在一定程度上起到监督作用。在设置前，需要先有自建应用，只有在该自建应用下的可见成员才具备接收应用消息通知的能力。
		','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			//array('name'=>'workerid','title'=>'企业成员','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'bumenid','title'=>'推送的部门','type'=>'select','options'=>'bumens','option_value'=>'bumenid'),
			array('name'=>'lookarea2','title'=>'查看员工数据','type'=>'string'),
			array('name'=>'sendtime','title'=>'推送时间','type'=>'string'),
			array('name'=>'content','title'=>'提醒文案','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'注册时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'name','title'=>'员工姓名','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','width'=>'15'),
		),	
	);
	$return['status']=array('未开启','已开启');
	$return['bumens']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'bumenid >'=>0,'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	//$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member_notice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['content']=$item['content']?json_decode($item['content'],true):array(
		'head'=>$this->workermsg_head,
		'buystatus0'=>$this->workermsg_buystatus0,
		'buystatus1'=>$this->workermsg_buystatus1,
		'buystatus2'=>$this->workermsg_buystatus2,
		'buystatus3'=>$this->workermsg_buystatus3,
	);
	$item['timebox']=$item['timebox']?json_decode($item['timebox'],true):array('time'=>array());
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
					//array('name'=>'workerid','title'=>'推送成员','type'=>'choose','action'=>'jy_scrm/worker.get_worker/','help'=>'发送应用消息通知给企业成员，查看成员的客户跟进情况'),
					array('name'=>'bumenid','title'=>'推送的部门','type'=>'select','options'=>'bumens','option_value'=>'bumenid'),
					array('type'=>'checkbox','title'=>'跟进数据','name'=>'lookarea','options'=>'lookarea','help'=>'设置查看跟进数据可见范围'),
					array('type'=>'array','title'=>'当天推送时间','name'=>'timebox','add_tip'=>'添加一个时间',
											'list'=>array(
												array('type'=>'string','name'=>'time','title'=>'时间段'),							
											),
											'details'=>array(
												array('type'=>'time_onehour','name'=>'time','title'=>'时间段'),
											)
					),
					array('type'=>'tip','title'=>'推送内容'),
					array('type'=>'text','title'=>'标头语','name'=>'content.head','help'=>'关键词变量：'.$this->workermsg_replaces),
					array('type'=>'text','title'=>'未跟进','name'=>'content.buystatus0','help'=>'关键词变量：'.$this->workermsg_replaces),
					array('type'=>'text','title'=>'跟进中','name'=>'content.buystatus1','help'=>'关键词变量：'.$this->workermsg_replaces),
					array('type'=>'text','title'=>'已拒绝','name'=>'content.buystatus2','help'=>'关键词变量：'.$this->workermsg_replaces),
					array('type'=>'text','title'=>'已成交','name'=>'content.buystatus3','help'=>'关键词变量：'.$this->workermsg_replaces),
					array('type'=>'switch','title'=>'是否开启','name'=>'status'),
		)
	);
	$return['bumens']=$this->getall('worker_group',array('merchid'=>$_SESSION['onemerchid'],'bumenid >'=>0,'uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
	$return['lookarea']=$this->work_lookareas;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'bumenid'=>intval($postdata['bumenid']),
		'workerid'=>intval($postdata['workerid']),
		'lookarea'=>$postdata['lookarea'],
		'timebox'=>ijson_encode($postdata['timebox']),
		'content'=>ijson_encode($postdata['content']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member_notice',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_scrm_member_notice',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member_notice',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_member_notice',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='upmember'){
	$userid='';
	$this->model('member')->batch();
}